//
//  DictionaryTest.m
//  CEDict
//
//  Created by Eric Nadeau on 07-06-02.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "DictionaryTest.h"


@implementation DictionaryTest

-(void) setUp
{
	NSArray* entries = [NSArray arrayWithObjects: 
					[[Entry alloc] 
						initWithSimplifed: [NSString stringWithCString: "你好"] 
						   andTraditional: [NSString stringWithCString: "你好"]
								andPinyin: [NSString stringWithCString: "ni3 hao3"]
							   andEnglish:
									[NSArray arrayWithObjects:
										@"hello", @"how are you", nil
									]
					],
					[[Entry alloc] 
							initWithSimplifed: [NSString stringWithCString: "词典"] 
							   andTraditional: [NSString stringWithCString: "詞典"]
									andPinyin: [NSString stringWithCString: "ci2 dian3"]
								   andEnglish:
									[NSArray arrayWithObjects:
										@"dictionary", nil
									]
					],
					[[Entry alloc] 
							initWithSimplifed: [NSString stringWithCString: "猫"] 
							   andTraditional: [NSString stringWithCString: "貓"]
									andPinyin: [NSString stringWithCString: "mao1"]
								   andEnglish:
									[NSArray arrayWithObjects:
										@"cat", @"pussy", nil
									]
					],
					nil
		];
	
	dict = [[Dictionary alloc] initWithEntries: entries];
}

-(void) testSearchFor: (NSString*) query expectedMatches: (int) count
{
	STAssertEquals( 
		(unsigned)count,
		[[dict search: query] count], 
		@"Searched dictionary for '%s', expecting %d matches.",
		query,
		count
	);
}

-(void) testSearch
{
	[self testSearchFor: @"ao" expectedMatches: 2];
	[self testSearchFor: @"cat" expectedMatches: 1];
	[self testSearchFor: @"dog" expectedMatches: 0];
}

-(void) testLoadFromFile
{
	Dictionary* d = [Dictionary loadFromFile: @"cedict_ts.u8"];
	NSArray* results = [d search: @"ni3 hao3"];
	
	STAssertEquals((unsigned)1, [results count], @"Checking number of matches, found %d.", [results count]);
	
	Entry* word = [results objectAtIndex:0];
	NSString* def1 = @"how are you";
	NSString* def2 = @"hello";
	STAssertTrue( [[word simplified] isEqualTo: [NSString stringWithCString: "你好"]], @"Checking simplified characters." );
	STAssertTrue( [[word traditional] isEqualTo: [NSString stringWithCString: "你好"]], @"Checking traditional characters." );
	STAssertTrue( [[word pinyin] isEqualTo: @"ni3 hao3"], @"Checking pinyin." );
	STAssertTrue(
				 (2 == [[word english] count])
				 &&
				 (
				  (
				   [def1 isEqualTo: [[word english] objectAtIndex:0]]
				   &&
				   [def2 isEqualTo: [[word english] objectAtIndex:1]]
				   )
				  ||
				  (
				   [def1 isEqualTo: [[word english] objectAtIndex:1]]
				   &&
				   [def2 isEqualTo: [[word english] objectAtIndex:0]]
				   )
				  ),
				 @"Verifying english definitions."
		);
}

@end
